<?php
class ComathangsController extends AppController {

	var $name = 'Comathangs';
    var $users=array('Comathangs','Donvis');
            

	function index() {
		$this->Comathang->recursive = 0;
		$this->set('comathangs', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid comathang', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('comathang', $this->Comathang->read(null, $id));
       
	}

	function add() { 	  
	  // debug($this->data);
        
       $d=0;
       $tong=0;
       //debug($this->data['Comathang']['dongia']);
      //  die();     
               
       if (!empty($this->data))
       {
        $dk=$this->data['Comathang']['mauvpp_id'];
        //debug($dk);
        $this->loadModel('Donvis');
        $tdv=$this->Donvis->Find('first',array('conditions'=>array('Donvis.id'=>$dk)),array('fields'=>'sotien'));
        $tien=$tdv['Donvis']['sotien'];
           foreach($this->data['Comathang']['soluong'] as $mathang => $sl)
           {
            
           // debug($sl);
          // die();
            
                if(!empty($sl))
               {
                 $this->Comathang->set(
                    array(
                        'id'=>'',
                        'mauvpp_id'=>($this->data['Comathang']['mauvpp_id']),
                        'mathang_id'=>$mathang,
                        'soluong'=>$sl, 
                    )  
                 );
                 
                  $tong=$tong+($this->data['Comathang']['gia'][$d]*$sl);   
                //  debug($tong); 
                //  debug($tien);  
                     if($tong<=$tien){
                        $this->Comathang->save();
                        $tien=$tien-$tong;
                        $this->Donvis->updateAll(array('sotien'=>$tien),array('Donvis.id'=>$dk));
                        
                     }else{
                        $this->Session->setFlash(__('tien cua don vi ko du de mua. Please, try again.', true));
                        $this->redirect(array('action'=>'add'));
                     }                       
               }
               $d++;
           }
        //   debug($tong);           
        $this->redirect(array('action'=>'index'));
        //  $this->flash($tong,array('action'=>'index'),10);
           //die();
       } 
       
       // die();
       
       $mauvpps = $this->Comathang->Mauvpp->find('list');
       $ten = $this->Comathang->Mathang->find('list',array('fields'=>'tenmathang'));
       $mathangs = $this->Comathang->Mathang->find('list');
       $gias = $this->Comathang->Mathang->find('list',array('fields'=>'dongia'));
       //ebug($gias);
       //debug($tenhangs);
       $this->set(compact('mauvpps', 'mathangs','gias','ten','tong'));
    }

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid comathang', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Comathang->save($this->data)) {
				$this->Session->setFlash(__('The comathang has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The comathang could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Comathang->read(null, $id);
		}
		$mauvpps = $this->Comathang->Mauvpp->find('list');
		$mathangs = $this->Comathang->Mathang->find('list');
        
		$this->set(compact('mauvpps', 'mathangs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for comathang', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Comathang->delete($id)) {
			$this->Session->setFlash(__('Comathang deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Comathang was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
